package batch;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;


public class wordcount {
    public static void main(String[] args) throws Exception {
        //执行离线计算DateSet,创建一个运行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
//        构造一份处理的数据
        DataSet<String> source_ = env.fromElements("I love beijing","I love china","bei jing is china");
//        tuple2 不同数据类型的集合                                                                    k2    v2
        DataSet<Tuple2<String,Integer>> mapout = source_.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            public void flatMap(String value, Collector<Tuple2<String,Integer>> out){
                String[] words = value.split(" ");
                for(String w:words){
                    out.collect(new Tuple2<String,Integer>(w,1));
                }
            }
        });
        DataSet<Tuple2<String,Integer>> reduceout = mapout.groupBy(0).sum(1);
        reduceout.print();



    }
}
